package com.banciyuan.bcywebview.biz.post.uploadpic;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.CoserApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.biz.post.uploadpic.services.UploadService;
import com.banciyuan.bcywebview.biz.write.photoselecotor.model.PhotoModel;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by squall on 2017/4/12.
 */

public class UploadAsyncHelper {

    private Context mContext;
    private String token;
    private RequestQueue queue;
    private boolean compress;
    private boolean cancel;

    public UploadAsyncHelper(Context mContext, boolean compress) {
        this.mContext = mContext;
        this.compress = compress;
        this.queue = VolleyQueue.getRquest(mContext);
    }

    public void getToken(final String tokenType, final String id, final String action, final List<PhotoModel> plist) {
        if (TextUtils.isEmpty(token)) {
            Response.Listener<String> onSuccToken = new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                        token = s;
                        startServices(token, plist);
                    }
                }
            };

            Response.ErrorListener onError = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            };

            String url = HttpUtils.BASE_URL + action;
            List<BcyPair> paramsList = new ArrayList<>();
            paramsList.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(mContext).getToken()));
            if (!TextUtils.isEmpty(tokenType) && !TextUtils.isEmpty(id)) {
                paramsList.add(new BcyPair(tokenType, id));
            }
            HashMap<String, String> map = HttpUtils.getData(paramsList);
            StringRequestParam request = new StringRequestParam(Request.Method.POST, url, map, onSuccToken, onError);
            queue.add(request);
        } else {
            startServices(token, plist);
        }
    }

    private void startServices(String token, List<PhotoModel> plist) {
        if (!cancel) {
            Intent intent = new Intent(mContext, UploadService.class);
            intent.putExtra("token", token);
            intent.putExtra("compress", compress);
            intent.putExtra("plist", (Serializable) plist);
            mContext.startService(intent);
        }
    }

    public void setCancel() {
        this.cancel = true;
    }
}
